package net.sample.gismeteo.gui;

import net.sample.gismeteo.Common;
import net.sample.gismeteo.svc.IWeatherServiceConnector;
import net.sample.gismeteo.svc.WeatherRetrievalService;
import android.app.ListActivity;
import android.content.ComponentName;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

public class ShowWeatherForecastListActivity extends ListActivity
{
	private static final String TAG = "ShowWeatherForecastListActivity";

	@Override
	protected void onCreate(Bundle pSavedInstanceState)
	{
		super.onCreate(pSavedInstanceState);
		startService(new Intent(this, WeatherRetrievalService.class));
		bindService(new Intent(this, WeatherRetrievalService.class), mServiceConnection, Context.BIND_AUTO_CREATE);

		Cursor lCursor = managedQuery(Common.CONTENT_URI, Common.PROJECTION, null, null, Common.DEFAULT_SORT_ORDER);
		setListAdapter(new ForecastListAdapter(this, lCursor));
	}

	
	
	@Override
	protected void onDestroy()
	{
		unbindService(mServiceConnection);
		super.onDestroy();
	}



	private ServiceConnection mServiceConnection = new ServiceConnection()
	{

		public void onServiceConnected(ComponentName name, IBinder service)
		{
			Log.d(TAG, "onServiceConnected");
			serviceConnector = IWeatherServiceConnector.Stub.asInterface(service);
		}

		public void onServiceDisconnected(ComponentName name)
		{
			Log.d(TAG, "onServiceDisconnected");
			serviceConnector = null;
		}

	};

	IWeatherServiceConnector serviceConnector = null;

	@Override
	protected void onListItemClick(ListView pL, View pV, int pPosition, long pId)
	{
		Uri lUri = ContentUris.withAppendedId(Common.CONTENT_URI, pId);
		startActivity(new Intent(Intent.ACTION_VIEW, lUri));
		//startActivity(new Intent("net.sample.action.showForecast", lUri));
	}

	
	
	
	
}
